/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.massfilerenamer.command;

import br.com.barrientosjunior.massfilerenamer.controller.StatusController;
import br.com.barrientosjunior.massfilerenamer.parser.PatternParser;
import br.com.barrientosjunior.massfilerenamer.parser.element.Element;
import br.com.barrientosjunior.massfilerenamer.util.UserFileFilter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 *
 * @author Junior
 */
public class RenameFilesCommand extends Thread{

    StatusController status;
    String pattern;
    String input;
    String output;
    String filter;

    public RenameFilesCommand(StatusController statusController, String pattern, String input, String output, String filter) {
        this.status = statusController;
        this.pattern = pattern;
        this.input = input;
        this.output = output;
        this.filter = filter;
    }

    public void execute() {
        this.start();
    }

    @Override
    public void run() {

        try {

            File folder = new File(input);
            File[] files = folder.listFiles(new UserFileFilter(filter));

            int totalFiles = files.length;
            this.status.setPercentStatus(0);

            List<Element> formattedPattern = new PatternParser().parse(pattern);

            for( int i = 0; i < files.length; i++ ){
                File file = files[i];
                StringBuilder newFileName = new StringBuilder();
                for ( Element element : formattedPattern){
                    element.setFile(file);
                    newFileName.append(element.toString());
                }

                if (!"".equals(output)){
                    File dest = new File(output + "\\" + newFileName.toString());

                    if (dest.exists())
                        dest.delete();

                    copy(file.getAbsolutePath(), dest.getAbsolutePath());
                    
                } else {
                    file.renameTo(new File(file.getParent() + newFileName.toString()));
                }

                int stats;
                
                if ( totalFiles == (i + 1) )
                    stats = 100;
                else
                    stats = (totalFiles/100) * i;

                status.setPercentStatus(stats);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Copy a file.
     * @param from
     * @param to
     * @throws IOException
     */
    public static void copy(String from, String to) throws IOException{
       InputStream in = null;
       OutputStream out = null;
       try {
          in = new FileInputStream(from);
          out = new FileOutputStream(to);
          while (true) {
             int data = in.read();
             if (data == -1) {
                break;
             }
             out.write(data);
          }
          in.close();
          out.close();
       } finally {
          if (in != null) {
             in.close();
          }
          if (out != null) {
             out.close();
          }
       }
    }

}
